﻿using DevExpress.ExpressApp.DC;
using DevExpress.Persistent.Base;
using DevExpress.Persistent.BaseImpl;
using DevExpress.Persistent.Validation;
using DevExpress.Xpo;
using EasyXaf.EntityDesigner.BusinessObjects.Entities;
using EasyXaf.EntityDesigner.BusinessObjects.Enums;
using System.ComponentModel;

namespace EasyXaf.EntityDesigner.BusinessObjects;

/// <summary>
/// 领域模型
/// </summary>
[XafDisplayName("领域模型")]
[XafDefaultProperty(nameof(Name))]
[NavigationItem]
public partial class DomainModel : BaseObject
{
	private string _name;
	private string _namespace;
	private string _version;
	private string _description;

	/// <summary>
	/// 名称
	/// </summary>
	[XafDisplayName("名称")]
	[RuleRequiredField]
	public string Name
	{
		get => _name;
		set => SetPropertyValue(nameof(Name), ref _name, value);
	}

	/// <summary>
	/// 命名空间
	/// </summary>
	[XafDisplayName("命名空间")]
	public string Namespace
	{
		get => _namespace;
		set => SetPropertyValue(nameof(Namespace), ref _namespace, value);
	}

	/// <summary>
	/// 版本
	/// </summary>
	[XafDisplayName("版本")]
	[RuleRequiredField]
	public string Version
	{
		get => _version;
		set => SetPropertyValue(nameof(Version), ref _version, value);
	}

	/// <summary>
	/// 描述
	/// </summary>
	[XafDisplayName("描述")]
	public string Description
	{
		get => _description;
		set => SetPropertyValue(nameof(Description), ref _description, value);
	}

	/// <summary>
	/// 实体
	/// </summary>
	[XafDisplayName("实体")]
	[Association("Entities-DomainModel")]
	[DevExpress.Xpo.Aggregated]
	public XPCollection<Entity> Entities
	{
		get => GetCollection<Entity>(nameof(Entities));
	}

	/// <summary>
	/// 枚举
	/// </summary>
	[XafDisplayName("枚举")]
	[Association("Enums-DomainModel")]
	[DevExpress.Xpo.Aggregated]
	public XPCollection<Enumeration> Enums
	{
		get => GetCollection<Enumeration>(nameof(Enums));
	}

	/// <summary>
	/// 引用
	/// </summary>
	[XafDisplayName("引用")]
	[DataSourceProperty(nameof(ReferenceModelsDataSource))]
	[Association("ReferenceDomainModels")]
	public XPCollection<DomainModel> ReferenceModels
	{
		get => GetCollection<DomainModel>(nameof(ReferenceModels));
	}

	/// <summary>
	/// ParentModels
	/// </summary>
	[Browsable(false)]
	[NonCloneable]
	[Association("ReferenceDomainModels")]
	public XPCollection<DomainModel> ParentModels
	{
		get => GetCollection<DomainModel>(nameof(ParentModels));
	}

	/// <summary>
	/// EntityDesigns
	/// </summary>
	[Browsable(false)]
	[Association("EntityDesigns-DomainModel")]
	[DevExpress.Xpo.Aggregated]
	public XPCollection<EntityDesign> EntityDesigns
	{
		get => GetCollection<EntityDesign>(nameof(EntityDesigns));
	}

	/// <summary>
	/// IsValid
	/// </summary>
	[Browsable(false)]
	[RuleFromBoolProperty(CustomMessageTemplate = "当前版本号已存在")]
	public bool IsValid
	{
		get => GetIsValid();
	}

	[Browsable(false)]
	public IList<DomainModel> ReferenceModelsDataSource
	{
		get => GetReferenceModelsDataSource();
	}

	public DomainModel(Session session)
		: base(session)
	{
	}

	private partial bool GetIsValid();

	private partial IList<DomainModel> GetReferenceModelsDataSource();
}
